import { Game, Scale, Types, WEBGL } from "phaser"
// import { LoadingScene, GameScene, UIScene } from './scenes'
import { GameScene } from './scenes/game'
import { LoadingScene} from './scenes/loading'
import { UIScene } from './scenes/ui'

type GameConfigExtended = Types.Core.GameConfig & {
  winScore: number
}
export const gameConfig: GameConfigExtended = {
  winScore: 0,
  type: WEBGL,
  parent: 'app',
  backgroundColor: '#9bd4c3',
  scale: {
    mode: Scale.ScaleModes.NONE,
    width: window.innerWidth,
    height: window.innerHeight,
  },
  physics: {
    default: 'arcade',//设置默认物理引擎为Arcade
    arcade: {
      debug: false,
    }
  },
  render: {
    antialiasGL: false,
    pixelArt: true
  },
  callbacks: {
    postBoot: ():void => {
      // console.log('执行postBoot回调在游戏引擎初始化后立即调用')
      sizeChanged()
    },
  },
  canvasStyle: `display: block; width: 100%; height: 100%;`,
  autoFocus: true,
  audio: {
    disableWebAudio: false, // 启用 Web Audio API
  },
  scene: [LoadingScene, GameScene, UIScene]
}

//让浏览器缩放的时候可以自适应
function sizeChanged() {
  if (window.game.isBooted) {
    setTimeout(() => {
      window.game.scale.resize(window.innerWidth, window.innerHeight);
      window.game.canvas.setAttribute(
        'style',
        `display: block;width:${window.innerWidth}px;height:${window.innerHeight}px;`
      );
    }, 100)
  }
}

window.onresize = () => sizeChanged()

window.game = new Game(gameConfig) //将游戏实例挂载到window对象
// console.log('查看物理引擎状态',window.game, 'window.game.physics', window.game.physics) //查看物理引擎状态
